
import { message, Upload } from 'antd';

export const ossUrl = 'https://kindstar-openapi.oss-cn-shanghai.aliyuncs.com/';

export const dirUrl = 'backend/report/'

export const beforeUpload = (file: any) => {
    // // 检查图片类型
    const isJPG = file.type === 'image/jpeg';
    const isPNG = file.type === 'image/png';
    const isPic = isJPG || isPNG

    if (!isPic) {
        message.error('支持格式：png、jpg、jpeg');
        return Upload.LIST_IGNORE
    }
    const isLt2M = file.size / 1024 / 1024 < 2;
    if (!isLt2M) {
        message.error('上传图片必须小于2MB!');
        return Upload.LIST_IGNORE
    }
    return (isPic && isLt2M) || Upload.LIST_IGNORE
}


//判断上传时请求头类型
export const getContentType = (fileName: string) => {
    let fileArr = fileName.split('.')
    let fileType = fileArr[fileArr.length - 1]
    if (fileType == 'jpg') {
        return 'image/jpg'
    }
    if (fileType == 'jepg') {
        return 'image/jpeg'
    }
    if (fileType == 'png') {
        return 'image/png'
    }

    if (fileType == 'pdf') {
        console.log("fileType", fileType)

        return 'application/pdf'
    }
    if (fileType == 'doc' || fileType == 'docx') {
        return 'application/msword'
    }
}


// 根据生日计算年龄
export const formatAge = (birthday: string) => {
    //  处理数据结构
    let result = birthday.split('-')
    // 新建日期对象
    let date = new Date()
    // 今天日期，数组，同 birthday
    let today = [date.getFullYear(), date.getMonth() + 1, date.getDate()]
    // 分别计算年月日差值
    let age = today.map((value: number, index: number) => {
        return Number(value) - Number(result[index])
    })
    // 当天数为负数时，月减 1，天数加上月总天数
    if (age[2] < 0) {
        // 简单获取上个月总天数的方法，不会错
        let lastMonth = new Date(today[0], today[1], 0)
        age[1]--
        age[2] += lastMonth.getDate()
    }
    // 当月数为负数时，年减 1，月数加上 12
    if (age[1] < 0) {
        age[0]--
        age[1] += 12
    }

    //  如果年份为0 但是月份为0 代表不足一年 则返回月份
    if (age[0] === 0 && age[1] !== 0) {
        return age[1] + '个月'
    } else if (age[0] == 0 && age[1] == 0) {
        // 代表不足一年 并且不足一个月 则返回天数
        return age[2] + '天'
    } else {
        return age[0] + '岁'
    }
}

// 时间格式化

export const formatTime = (time: Date) => {
    if (!time) return time
    let date = new Date(time);
    let Y = date.getFullYear() + '-';
    let M = (date.getMonth() + 1 < 10 ? '0' + (date.getMonth() + 1) : date.getMonth() + 1) + '-';
    let D = (date.getDate() < 10 ? '0' + date.getDate() : date.getDate());
    // let h = (date.getHours() < 10 ?  '0'+ date.getHours() : date.getHours()) + ':';
    // let m = (date.getMinutes() < 10 ?  '0'+ date.getMinutes() : date.getMinutes()) + ':';
    // let s = (date.getSeconds() < 10 ?  '0'+ date.getSeconds() : date.getSeconds()); 
    return (Y + M + D)
}


//输入内容时去除空格
export const getValueFromEvent = (e: any) => {
    return e.target?.value?.replace(/(^\s*)|(\s*$)/g, '')
}

